package com.ytktt.study.week9.task3.rpcfx.server.netty.coder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * fastjson netty编码器
 *
 * @author ytk
 */
public class FastJsonEncoder extends MessageToByteEncoder<Object> {

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        byte[] payload = JSON.toJSONBytes(msg, SerializerFeature.WriteClassName);
        out.writeBytes(payload);
    }
}
